package com.px.pa.modulars.circle.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.annotation.Inner;
import com.px.pa.modulars.circle.entity.AuditConfiguration;
import com.px.pa.modulars.circle.entity.CircleCompany;
import com.px.pa.modulars.circle.entity.CircleUser;
import com.px.pa.modulars.circle.service.AuditConfigurationService;
import com.px.pa.modulars.circle.service.CircleCompanyService;
import com.px.pa.modulars.circle.service.CircleUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequiredArgsConstructor
@RequestMapping("/circlecompany" )
@Api(value = "circlecompany", tags = "管理")
public class CircleCompanyController {

    private final CircleCompanyService circleCompanyService;
    private final CircleUserService circleUserService;

    private final AuditConfigurationService auditConfigurationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param circleCompany
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getCircleCompanyPage(Page page, CircleCompany circleCompany) {

        Page<CircleCompany> pageList= circleCompanyService.page(page, new QueryWrapper<CircleCompany>()
                .ne("display",1)
                .orderByDesc("create_time"));


        pageList.getRecords().forEach(a->{
          CircleUser user  =  circleUserService.getById(a.getUid());
          if (user!=null && user.getName()!=null){
              a.setUserName(user.getName());
          }
        });


        return R.ok(pageList);
    }

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        CircleCompany circleCompany =  circleCompanyService.getById(id);

        if (circleCompany!=null && circleCompany.getUid() !=null){
            CircleUser user  =  circleUserService.getById(circleCompany.getUid());
            if (user!=null && user.getName()!=null){
                circleCompany.setUserName(user.getName());
            }
        }
        return R.ok(circleCompany);
    }




    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping
    @Inner(value = false)
    public R updateById(@RequestBody CircleCompany company) {

        CircleCompany circleCompany = circleCompanyService.getById(company.getId());
        circleCompany.setDisplay(company.getDisplay());
        return R.ok( circleCompanyService.updateById(circleCompany));
    }


    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @GetMapping("/del/{id}" )
    @Inner(value = false)
    public R removeById(@PathVariable Integer id) {
        return R.ok(circleCompanyService.removeById(id));
    }




}
